using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLThuChi.Repositories
{
	#region using
	using QLThuChi.Models;
	#endregion
	public class GiaiDoanRepository
	{
		public static List<GiaiDoan> SelectAll()
		{
			return DataContext.Instance.GiaiDoans.OrderBy(item => item.TenGiaiDoan).ToList();
		}

		public static GiaiDoan SelectByID(int magiaidoan)
		{
			return DataContext.Instance.GiaiDoans.FirstOrDefault(item => item.MaGiaiDoan == magiaidoan );
		}

		public static bool Insert(GiaiDoan obj)
		{
			try
			{
				DataContext.Instance.GiaiDoans.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(int magiaidoan)
		{
			try
			{
				var delitem = DataContext.Instance.GiaiDoans.FirstOrDefault(item => item.MaGiaiDoan == magiaidoan );
				DataContext.Instance.GiaiDoans.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<GiaiDoan> RetrieveByID(int magiaidoan)
		{
			return (from item in DataContext.Instance.GiaiDoans where  item.MaGiaiDoan == magiaidoan  select item).ToList();
		}

	}
}